/*
UTMPlayer1.0
www.usualtool.com
292951110@qq.com
@HuangDou.China.2020.12
*/
function UTMPlayer(container='music-player',playlist='musiclist',play='play',paused='paused',loop='loop',pre='preurl',next='nexturl') {
	var container;
	var playlist;
	var play;
	var paused;
	var loop;
	var pre;
	var next;
	var audiostr='<div class="audiobody"><audio controls autoplay="autoplay" controlsList="nodownload" oncontextmenu="return false"><source src=""></source>Browser does not support.</audio>';
	audiostr+='<p align=center><button id="'+play+'">Play</button> ';
	audiostr+='<button id="'+paused+'">Paused</button> ';
	audiostr+='<button id="'+pre+'">Pre</button> ';
	audiostr+='<button id="'+next+'">Next</button> ';
	audiostr+='<button id="'+loop+'">Loop</button></p></div>';
	$("#"+container+"").html(audiostr);
	var listindex = 1;
	len = $("#"+playlist+" li").length;
		audio = $("audio")[0];
		source=$("source")[0];
	var playaudio = function() {
        try {
		    audio.play(); 
		}catch(e){
            console.log(e);
        }
	};
	var changeSrc = function(index) {
	var index;
		$("#"+playlist+" li").removeClass("active");
		$("#"+playlist+" li").removeClass("over");
		audio.src=$.base64.decode($("#playlist"+index+"").data("src"));       
 		var lrcurl=$("#playlist"+index+"").data("lrc");
        if(lrcurl.length==0){
            createLrc(0);
        }else{
            createLrc(lrcurl);
        }
		$("#"+paused+"").trigger("click");
		$("#"+playlist+" li").eq(index).addClass("active");
		playaudio();
	};
	changeSrc(listindex);
	$("audio").on("ended", function() {
		if(++listindex > len) {
		    listindex = 1;
		}
		changeSrc(listindex);
	});
	$("#"+playlist+"").on("mouseover", "li", function() {
		$(this).addClass("over");
	});
	$("#"+playlist+"").on("mouseout", "li", function() {
		$(this).removeClass("over");
	});
	$("#"+playlist+"").on("click", "li", function() {
		listindex = $(this).attr("data-id");
		changeSrc(listindex);
	});
	$("#"+play+"").click(playaudio);
	$("#"+paused+"").click(function() {
		audio.pause();
	});
	$("#"+loop+"").click(function() {
		var val=audio.loop==true?"Loop play":"Close loop";
		$(this)[0].innerText=val;
		audio.loop = !audio.loop;
	});
	$("#"+pre+"").click(function() {
		if(--listindex < 1) {
            listindex = 1;
            return;
		} else {
		    changeSrc(listindex);
		}
	});
	$("#"+next+"").click(function() {
		if(++listindex > len) {
            listindex = len;
            return;
		} else {
		    changeSrc(listindex);
		}
	});
}
function createLrc(lrcurl) {
    if(lrcurl===0){
        $("#lrc li").remove();
        $("#lrc").append("<li style='list-style: none;'>No lyric!</li>");
    }else{
        $("#lrc li").remove();
        $.ajax({url:lrcurl,contentType:"application/x-www-form-urlencoded",success:function(result){
            var medisArray = new Array(); 
            var medis =result;
            var medises = medis.split("\n"); 
            $.each(medises, function (i, item) {
                var t = item.substring(item.indexOf("[") + 1, item.indexOf("]"));
                medisArray.push({
                    t: (t.split(":")[0] * 60 + parseFloat(t.split(":")[1])).toFixed(3),
                    c: item.substring(item.indexOf("]") + 1, item.length)
                });
            });
            var ul = $("#lrc");
            $.each(medisArray, function (i, item) {
                var li = $("<li style='list-style: none;'>");
                li.html(item.c);
                ul.append(li);
            })
            var lineNo = 5;
            audio.ontimeupdate = function () {
                if (lineNo == medisArray.length - 1 && audio.currentTime.toFixed(3) >= parseFloat(medisArray[lineNo].t)) {
                    lineHeight(lineNo);
                }
                if (parseFloat(medisArray[lineNo].t) <= audio.currentTime.toFixed(3) &&
                audio.currentTime.toFixed(3) <= parseFloat(medisArray[lineNo + 1].t)) {
                    lineHeight(lineNo);
                    lineNo++;
                }
            }
        }})
    }
}
function lineHeight(lineno){
    var fraction = 0.5;
    var topNum = 0;
    var ul = $("#lrc");
    var $ul = document.getElementById("lrc");
    if (lineno > 0) {
        $(ul.find("li").get(topNum + lineno - 1)).removeClass("lineheight");
    }
    var nowline = ul.find("li").get(topNum + lineno);
    $(nowline).addClass("lineheight");
    var _scrollTop;
    $ul.scrollTop = 0;
    if ($ul.clientHeight * fraction > nowline.offsetTop) {
        _scrollTop = 0;
    } else if (nowline.offsetTop > ($ul.scrollHeight - $ul.clientHeight * (1 - fraction))) {
        _scrollTop = $ul.scrollHeight - $ul.clientHeight;
    } else {
        _scrollTop = nowline.offsetTop - $ul.clientHeight * fraction;
    }
    if ((nowline.offsetTop - $ul.scrollTop) >= $ul.clientHeight * fraction) {
        $ul.scrollTop += Math.ceil(nowline.offsetTop - $ul.scrollTop - $ul.clientHeight * fraction);
    } else if ((nowline.offsetTop - $ul.scrollTop) < $ul.clientHeight * fraction && _scrollTop != 0) {
        $ul.scrollTop -= Math.ceil($ul.clientHeight * fraction - (nowline.offsetTop - $ul.scrollTop));
    } else if (_scrollTop == 0) {
        $ul.scrollTop = 0;
    } else {
        $ul.scrollTop += $(ul.find('li').get(0)).height();
}
}